package org.bootlimn.common;

/**
 * @author satya
 * This class defines the structure of an IO block as outputted by blockioinfo 
 * and functions to access the individual attributes
 *
 */

public class Ioblock {

	private String type; 		// whether this was a request for starting IO or end
	private int time;			// timestamp	
	private String devname;		// block device name
	private int ino;			// i-node number of the mapped file
	private int error;			// 0 on success
	private String sector;		// beginning sector for the entire bio
	private int flags;			/* 
	 							*  	BIO_UPTODATE    0       ok after I/O completion
	 							*  	BIO_RW_BLOCK    1       RW_AHEAD set, and read/write would block
	 							*  	BIO_EOF         2       out-out-bounds error
	 							*  	BIO_SEG_VALID   3       nr_hw_seg valid 
	 							*  	BIO_CLONED      4       doesn't own data
	 							*  	BIO_BOUNCED     5       bio is a bounce bio
	 							*  	BIO_USER_MAPPED 6       contains user pages
	 							*  	BIO_EOPNOTSUPP  7       not supported
	 							*/
	private int rw; 			// binary trace for read/write request
								// 0 for read 1 for write
	private int vcnt;			// bio vector count which represents number of array element (page, 
 								// offset, length) which make up this I/O request
	private int idx;			// offset into the bio vector array
	private int phys_segments;	// number of segments in this bio after physical address
 								// coalescing is performed.  
	private int hw_segments;	// number of segments after physical and DMA remapping
 								// hardware coalescing is performed
	private int size;			// total size in bytes
	private String bdev;		// target block device
	private String bdev_contains;	// points to the device object which contains the
								// partition (when bio structure represents a partition)
	private String p_start_sect;	// points to the start sector of the partition
								// structure of the device
	

	// constructor

	public Ioblock(String type, int time,String devname, int ino, int error, String sector, int flags, int rw,int vcnt, int idx, int      phys_segments, int hw_segments,  int size, String bdev, String bdev_contains, String p_start_sect) {
		this.type = type;
		this.time = time;
		this.devname = devname;
          	this.ino = ino;
          	this.error = error;
	  	this.sector = sector;
         	this.flags = flags;
       	   	this.rw = rw;
	  	this.vcnt = vcnt;
	  	this.idx = idx;
           	this.phys_segments = phys_segments;
          	this.hw_segments = hw_segments;
           	this.size = size;
           	this.bdev = bdev;
	   	this.bdev_contains = bdev_contains;
	  	this.p_start_sect = p_start_sect;	
	}
	
	//functions to access individual attributes

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
   
	public int getTime() {
		return time;
	}

	public void setTime(int time) {
		this.time = time;
	}
	
	public String getDevname() {
		return devname;
	}
 
	public void setDevname(String devname) {
		this.devname = devname;
	}

	public int getIno() {
		return ino;
	}
       
        public void setIno(int ino) {
		this.ino = ino;
	}

	public int getError() {
		return error;
	}
       
        public void setError(int error) {
		this.error = error;
	}

	public String getSector() {
		return sector;
	}
       
        public void setSector(String sector) {
		this.sector = sector;
	}

	public int getFlags() {
		return flags;
	}
       
        public void setFlags(int flags) {
		this.flags = flags;
	}

	public int getRw() {
		return rw;
	}
       
        public void setRw(int rw) {
		this.rw = rw;
	}

	public int getVcnt() {
		return vcnt;
	}
       
        public void setVcnt(int vcnt) {
		this.vcnt = vcnt;
	}

	public int getIdx() {
		return idx;
	}
       
        public void setIdx(int idx) {
		this.idx = idx;
	}

	public int getPhys_segments() {
		return phys_segments;
	}
       
        public void setPhys_segments(int phys_segments) {
		this.phys_segments = phys_segments;
	}

	public int getHw_segments() {
		return hw_segments;
	}
       
        public void setHw_segments(int hw_segments) {
		this.hw_segments = hw_segments;
	}

	public int getSize() {
		return size;
	}
       
        public void setSize(int size) {
		this.size = size;
	}

	public String getBdev() {
		return bdev;
	}
       
        public void setBdev(String bdev) {
		this.bdev = bdev;
	}

	public String getBdev_contains() {
		return bdev_contains;
	}
       
        public void setBdev_contains(String bdev_contains) {
		this.bdev_contains = bdev_contains;
	}

	public String getP_start_sect() {
		return p_start_sect;
	}
        
        public void setP_start_sect(String p_start_sect) {
		this.p_start_sect = p_start_sect;
	}

	// To print it all out (temporary for text output purpose)
	// depending on whether it was a request or end of a blockIO the feilds change
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("Block IO Details - ");
        	if(getType()=="request")
		{
        		sb.append("Request:");
			sb.append(", ");
			sb.append("Time:" + getTime());
			sb.append(", ");
			sb.append("Devname:" + getDevname());
			sb.append(", ");
			sb.append("Ino:" + getIno());
			sb.append(", ");
			sb.append("Sector:" + getSector());
			sb.append(", ");
			sb.append("Flags:" + getFlags());
			sb.append(", ");
			if( getRw() == 0)
				sb.append("Read");
			else
				sb.append("Write");
			sb.append(", ");
			sb.append("Vcnt:" + getVcnt());
			sb.append(",");
        	        sb.append("Idx:" + getIdx());
			sb.append(",");
			sb.append("Phys_segments:" + getPhys_segments());
			sb.append(",");
			sb.append("Hw_segments:" + getHw_segments());
			sb.append(",");
			sb.append("Size:" + getSize());
			sb.append(", ");
			sb.append("Bdev:" + getBdev());
			sb.append(", ");
			sb.append("Bdev_contains:" + getBdev_contains());
			sb.append(", ");
			sb.append("P_start_sect:" + getP_start_sect());
			sb.append(".");
		}
		else
		{
			sb.append("End:");
			sb.append(", ");
			sb.append("Time:" + getTime());
			sb.append(", ");
			sb.append("Devname:" + getDevname());
			sb.append(", ");
			sb.append("Ino:" + getIno());
			sb.append(", ");
			sb.append("Error:" + getError());
			sb.append(", ");
			sb.append("Sector:" + getSector());
			sb.append(", ");
			sb.append("Flags:" + getFlags());
			sb.append(", ");
			if( getRw() == 0)
				sb.append("Read");
			else
				sb.append("Write");
			sb.append("Vcnt:" + getVcnt());
			sb.append(",");
        	        sb.append("Idx:" + getIdx());
			sb.append(",");
			sb.append("Phys_segments:" + getPhys_segments());
			sb.append(",");
			sb.append("Hw_segments:" + getHw_segments());
			sb.append(",");
			sb.append("Size:" + getSize());
			sb.append(".\n");
		}	
		  return sb.toString();
	}

}

